package com.x.uikit.channel.feed;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.x.uikit.link.XLink;
import com.x.uikit.multistateview.IFeedLoadMoreView;

public class XFeedView extends RecyclerView {
    protected XFeedAdapter mAdapter;
    private IFeedLoadMoreView mLoadMoreView;

    public XFeedView(@NonNull Context context) {
        super(context);
        mAdapter = new XFeedAdapter();
        setAdapter(mAdapter);
        addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && mLoadMoreView != null && recyclerView.getAdapter() != null) {
                    LayoutManager manager = recyclerView.getLayoutManager();
                    int position = 0;
                    if (manager instanceof LinearLayoutManager) {
                        LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
                        position = layoutManager.findLastVisibleItemPosition();
                    } else if (manager instanceof StaggeredGridLayoutManager) {
                        StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager;
                        int[] positions = layoutManager.findLastVisibleItemPositions(null);
                        if (positions != null) {
                            for (int poi : positions) {
                                if (poi > position) {
                                    position = poi;
                                }
                            }
                        }
                    } else if (manager instanceof GridLayoutManager) {
                        GridLayoutManager layoutManager = (GridLayoutManager) manager;
                        position = layoutManager.findLastVisibleItemPosition();
                    }
                    if (position >= recyclerView.getAdapter().getItemCount() - 1) {
                        mLoadMoreView.onLoadMore();
                    }
                }
            }
        });
    }

    public void setDataSource(IXFeed.DataSource dataSource) {
        mAdapter.setDataSource(dataSource);
    }

    public void setCardFactory(IXFeed.CardFactory cardFactory) {
        mAdapter.setCardFactory(cardFactory);
    }

    public void setLoadMoreView(IFeedLoadMoreView view) {
        mLoadMoreView = view;
        mAdapter.setLoadMoreView(view);
    }

    public void notifyDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }

    public void setEventObserver(XLink.EventObserver observer) {
        mAdapter.setEventObserver(observer);
    }
}
